/**
 * Created by on 2018/5/9.
 */
define(function () {

    var self,
        content,
        app = win.app,
        others = {
            ischecked: '0',
            countAmount: 0
        },
        subData = {},
        address = {
            GWAK030: '', //客户手机号
            GWBG002: '',//客户编号
            GWAK028: '',//详细地址
            GWAKL004: ''//客户名称
        },
        jxsaddress = [],
        theme = {
            GWBGL001: '0',//商机主题编号
            GWBGL003: ''//商机主题
        },
        checkbox = {
            GWBG003: 'N',//特价否
            GWBG004: 'N'//信用否
        },
        ywy = {
            GWBG005: '',
            GWBG005_NAME: '',
            MOBILE: ''
        },
        pageParameters,
        userInfo
    ;

    var OrderTemp = app.Module.extend({

        init: function () {
            self = this;
            self._super({
                _readyBefore: function () {
                    pageParameters = app.pageParameters['listadd_page'];

                    userInfo = app.userInfos.sysUser;
                }
            }, win);

        },
        _lazy: function () {
            this._super();
            if (pageParameters && pageParameters.type == 'sj-info') {
                var item = pageParameters.item;
                theme.GWBGL001 = item.GWBGL001;
                theme.GWBGL003 = item.GWBGL003;
                address.GWAK030 = item.GWAK030;
                address.GWBG002 = item.GWBG002;
                address.GWAK028 = item.GWAK028;
                address.GWAKL004 = item.GWAKL004;
                checkbox.GWBG003 = item.GWBG003;
                checkbox.GWBG004 = item.GWBG004;

                ywy.GWBG005 = item.GWBG005;
                ywy.GWBG005_NAME = item.GWBG005_NAME;
                ywy.MOBILE = item.MOBILE;

                app.ajax({
                    xurl: "api/wx/gwat153/jxsList",
                    loading: true,
                    xdata: {
                        GWBE001: content.theme.GWBGL001,
                    },
                    cb: function (result) {
                        if (result.ok) {
                            jxsaddress.pushArray(result.data.data.addMap({checked: '1'}, true), true);
                        }
                    }
                });
            }
            if (pageParameters && pageParameters.type == 'sjzt-add') {
                var item = pageParameters.kh;
                address.GWAK030 = item.GWAK030;
                address.GWBG002 = item.GWAK001;
                address.GWAK028 = item.GWAK028;
                address.GWAKL004 = item.GWAKL004;
            }

            if (!ywy.GWBG005) {
                ywy.GWBG005 = userInfo.ID;
                ywy.GWBG005_NAME = userInfo.NAME;
                ywy.MOBILE = userInfo.MOBILE;
            }

            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });

        },
        _ready: function () {
            content = new Vue({
                el: app.getByid(doc, 'content'),
                data: function () {
                    return {
                        theme: theme,//商机主题
                        address: address,
                        item: checkbox,
                        jxsaddress: jxsaddress,
                        ywy: ywy
                    }
                },
                methods: {
                    checkDeleteTheme: function () { //检查是否存在记录  存在不能删除
                        app.ajax({
                            xurl: "api/wx/gwat153/checkTheme",
                            loading: true,
                            xdata: {
                                GWBHNAME: content.theme.GWBGL001,
                                GWBHID: content.address.GWBG002
                            },
                            cb: function (result) {
                                if (result.ok) {
                                    var count = result.data.data.count;
                                    if (count > 0) {
                                        app.alert({
                                            msg: '拜访记录已经存在，无法删除'
                                        });
                                    } else {
                                        content.deleteTheme();
                                    }
                                }
                            }
                        });
                    },
                    deleteTheme: function () {
                        app.ajax({
                            xurl: "api/wx/gwat153/deleteTheme",
                            loading: true,
                            xdata: {
                                GWBE001: content.theme.GWBGL001
                            },
                            cb: function (result) {
                                if (result.ok) {
                                    pageParameters.cb();
                                    app.alert({
                                        msg: '删除成功！',
                                        ok: function () {
                                            app.back(1);
                                        }
                                    });

                                }
                            }
                        })
                    },
                    checkSaveTheme: function () {//检查商机主题是否存在拜访记录   存在不可修改
                        if (content.theme.gwbgl001 == '0') { //新增 验证商机主题是否存在
                            content.checkExist();
                        } else {
                            app.ajax({
                                xurl: "api/wx/gwat153/checkTheme",
                                loading: true,
                                xdata: {
                                    GWBHNAME: content.theme.GWBGL003,
                                    GWBHID: content.address.GWBG002
                                },
                                cb: function (result) {
                                    if (result.ok) {
                                        var count = result.data.data.count;
                                        if (count > 0) {
                                            app.alert({
                                                msg: '拜访记录已经存在，无法修改'
                                            });
                                        } else {
                                            content.subOrder();
                                        }
                                    }
                                }
                            });
                        }

                    },
                    checkExist: function () {//检查商机主题是否存在
                        // app.ajax({
                        //     xurl: "sdhn/gwat153/checkExist",
                        //     loading: true,
                        //     xdata: {
                        //         gwbgl003: content.theme.gwbgl003,
                        //
                        //     },
                        //     cb: function (result) {
                        //         if (result.ok) {
                        //             var count = result.data.data.count;
                        //             if (count > 0) {
                        //                 app.alert({
                        //                     msg: '商机主题已经存在，无法新增'
                        //                 });
                        //             } else {
                                        content.subOrder();
                        //             }
                        //         }
                        //     }
                        // });
                    },
                    showCustomers: function () {//客户
                        app.openPage({
                            src: 'hn-bfjltb/customers.html',
                            args: {
                                cb: function (obj) {
                                    content.address.GWAK030 = obj.GWAK030;
                                    content.address.GWBG002 = obj.GWAK001;
                                    content.address.GWAK028 = obj.GWAK028;
                                    content.address.GWAKL004 = obj.GWAKL004;
                                }
                            }
                        });
                    },
                    showYwy: function () {//业务员
                        app.openPage({
                            src: 'base/ywy.html',
                            args: {
                                cb: function (obj) {
                                    content.ywy.GWBG005 = obj.ID;
                                    content.ywy.GWBG005_NAME = obj.NAME;
                                    content.ywy.MOBILE = obj.MOBILE;
                                }
                            }
                        });
                    },
                    showJxs: function () { //经销商
                        app.openPage({
                            src: "gwat153/gwat153-jxs-info.html",
                            argsKey: 'jxsinfo_page',
                            args: {
                                jxschoose: app.cloneJSON(jxsaddress),
                                cb: function (details) {
                                    jxsaddress.pushArray(details, true);
                                    //content.jxsaddress.pushArray(details,true);
                                }
                            }
                        });
                    },
                    subOrder: function () {

                        if (!content.address.GWBG002 || content.address.GWBG002 == '') {
                            app.toast('请选择客户！');
                            return false;
                        }
                        // if (!content.jxsaddress[0]) {
                        //     app.toast('请选择经销商！');
                        //     return false;
                        // }
                        // if (!content.ywy.gwbg005 || content.ywy.gwbg005 == '') {
                        //     app.toast('请选择业务员！');
                        //     return false;
                        // }

                        app.confirm({
                            msg: '确定是否提交？',
                            ok: function () {
                                var _data = {
                                    GWBGL001: content.theme.GWBGL001,//商机主题编号
                                    GWBGL003: content.theme.GWBGL003,//商机主题
                                    GWBG002: content.address.GWBG002,
                                    GWBG003: checkbox.GWBG003,
                                    GWBG004: checkbox.GWBG004,
                                    GWBG005: content.ywy.GWBG005,
                                    CHARACTERISTICS: [] //选择的经销商列表
                                };
                                var info = content.jxsaddress;
                                for (var i = 0; i < info.length; i++) {
                                    _data.CHARACTERISTICS.push({
                                        GWBE002: info[i].GWAK001,//经销商编号
                                        GWBE003: info[i].GWBE003//是否审核
                                    })
                                }
                                app.ajax({
                                    xurl: "api/wx/gwat153/gwat153Save",
                                    xdata: {
                                        obj: app.jsonString(_data)
                                    },
                                    cb: function (result) {
                                        if (result.ok) {
                                            pageParameters.cb();

                                            app.alert({
                                                msg: '提交成功！',
                                                ok: function () {
                                                    app.back(1);
                                                }
                                            });
                                            // if(pageParameters&&pageParameters.type=='sj-info'){
                                            //     pageParameters.cb();
                                            // }
                                        }
                                    }
                                });
                            }

                        });

                    }
                }
            })
        }
    });

    return new OrderTemp();

});